﻿/*
 * @Autor: PK
 */

using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace ShkSimulation.core.util {
	public class WindowApiHelper {

		//定义内存的信息结构
		[StructLayout(LayoutKind.Sequential)]
		public struct MemoryInfo {
			public uint dwLength; //当前结构体大小
			public uint dwMemoryLoad; //当前内存使用率
			public ulong ullTotalPhys; //总计物理内存大小
			public ulong ullAvailPhys; //可用物理内存大小
			public ulong ullTotalPageFile; //总计交换文件大小
			public ulong ullAvailPageFile; //总计交换文件大小
			public ulong ullTotalVirtual; //总计虚拟内存大小
			public ulong ullAvailVirtual; //可用虚拟内存大小
			public ulong ullAvailExtendedVirtual; //保留 这个值始终为0
		}

		[DllImport("kernel32")]
		[return: MarshalAs(UnmanagedType.Bool)]
		public static extern bool GlobalMemoryStatusEx(ref MemoryInfo mi); //获取内存相关数据


		/// <summary>
		/// 获取电脑磁盘剩余空间
		/// </summary>
		/// <returns></returns>
		public static string GetDrivers() {
			string result = "";
			DriveInfo[] drives = DriveInfo.GetDrives();
			foreach (DriveInfo drive in drives) {
				if (!drive.IsReady)
					continue;
				if (result.Length > 0) result += ",";
				result += string.Format("{0}盘剩余{1}", drive.Name.Split(':')[0] + "盘剩余",
					FormatSize(drive.TotalFreeSpace));
			}
			return result;
		}

		/// <summary>
		/// 获取电脑IP地址
		/// </summary>
		/// <returns></returns>
		public static string GetIPAddress() {
			string hostName = Dns.GetHostName();
			IPAddress[] iPs = Dns.GetHostEntry(hostName).AddressList;
			foreach (IPAddress ip in iPs) {
				if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
					return ip.ToString();
				}
			}

			return "";
		}

		/// <summary>
		/// 获取内存实时占用率
		/// </summary>
		/// <returns></returns>
		public static string GetMemoryUtilization() {
			return String.Format("内存实时占用{0}/{1}", FormatSize(GetUsedPhys()), FormatSize(GetTotalPhys()));
		}

		/// <summary>
		/// 获得当前内存使用情况
		/// </summary>
		/// <returns></returns>
		public static MemoryInfo GetMemoryStatus() {
			MemoryInfo mi = new MemoryInfo();
			mi.dwLength = (uint) Marshal.SizeOf(mi);
			GlobalMemoryStatusEx(ref mi);
			return mi;
		}

		/// <summary>
		/// 获得当前可用物理内存大小
		/// </summary>
		/// <returns>当前可用物理内存（B）</returns>
		public static ulong GetAvailPhys() {
			MemoryInfo mi = GetMemoryStatus();
			return mi.ullAvailPhys;
		}

		/// <summary>
		/// 获得当前已使用的内存大小
		/// </summary>
		/// <returns>已使用的内存大小（B）</returns>
		public static ulong GetUsedPhys() {
			MemoryInfo mi = GetMemoryStatus();
			return (mi.ullTotalPhys - mi.ullAvailPhys);
		}

		/// <summary>
		/// 获得当前总计物理内存大小
		/// </summary>
		/// <returns>总计物理内存大小（B）</returns>
		public static ulong GetTotalPhys() {
			MemoryInfo mi = GetMemoryStatus();
			return mi.ullTotalPhys;
		}

		/// <summary>
		/// 格式化容量大小
		/// </summary>
		/// <param name="size">容量（B）</param>
		/// <returns>已格式化的容量</returns>
		private static string FormatSize(double size) {
			double d = size;
			int i = 0;
			while ((d > 1024) && (i < 5)) {
				d /= 1024;
				i++;
			}
			string[] unit = {"B", "KB", "MB", "G", "T"};
			return (string.Format("{0} {1}", Math.Round(d, 2), unit[i]));
		}
	}
}